#include "analyzer_common.h"

int analyzer_droprate(sparse_matrix_t* s, int block_order){ 
    printf("analyzer_droprate block_order=%d\n", block_order);

    sparse_pointer_t nnz_submtx = 0;
    sparse_index_t n_order = s->m / block_order;
    sparse_pointer_t nnz_each_block_capacity = 0;
    sparse_pointer_t nnz_each_block_len = 0;
    int* nnz_each_block = malloc(sizeof(int) * 1000);
    for(sparse_index_t brow = 0; brow < n_order; brow++){
        for(sparse_pointer_t idx = s->rowpointer[brow * block_order]; idx < s->rowpointer[brow * block_order + 1]; idx+=block_order){
            if(s->columnindex[idx]%block_order){
                printf("[Warning] s->columnindex[idx]%%block_order != 0\n");
                continue;
            }
            int nnz_effective_current_block = 0;
            sparse_pointer_t inrow_offset = idx - s->rowpointer[brow * block_order];
            for(int row_in_block = 0; row_in_block < block_order; row_in_block++){
                sparse_pointer_t row_pointer = s->rowpointer[brow * block_order + row_in_block];
                for(int col_in_block = 0; col_in_block < block_order; col_in_block++){

                    // if(row_in_block == 0 && col_in_block == 0){ // 抽左上角
                    //     continue;
                    // }
                    if(row_in_block == 0 && col_in_block == 0){
                        if(fabs(s->value[row_pointer + inrow_offset + col_in_block]) > 1e-100){
                            nnz_submtx++;
                        }
                    }
                    
                    if(fabs(s->value[row_pointer + inrow_offset + col_in_block]) > 1e-100){
                        nnz_effective_current_block++;
                    }
                }
            }
            if(nnz_each_block_len >= nnz_each_block_capacity){
                nnz_each_block = realloc(nnz_each_block, sizeof(int)*(nnz_each_block_capacity+1000));
                nnz_each_block_capacity += 1000;
            }
            nnz_each_block[nnz_each_block_len] = nnz_effective_current_block;
            nnz_each_block_len++;
        }
    }

    // FILE* output_file = fopen("analyzer_droprate_output.txt", "w");
    // for(sparse_pointer_t blkidx = 0; blkidx < nnz_each_block_len; blkidx++){
    //     fprintf(output_file, "%d\n", nnz_each_block[blkidx]);
    // }
    // fclose(output_file);

    sparse_pointer_t* each_nnz_block_count = malloc(sizeof(sparse_pointer_t) * (block_order * block_order + 1));
    memset(each_nnz_block_count, 0, sizeof(sparse_pointer_t) *  (block_order * block_order + 1));
    for(sparse_pointer_t blkidx = 0; blkidx < nnz_each_block_len; blkidx++){
        each_nnz_block_count[nnz_each_block[blkidx]]++;
    }

    printf("non-zero entries of SUBMATRIX : %llu\n", nnz_submtx);
    printf("non-zero entries count, tile count\n");
    for(int i=0;i<=block_order * block_order;i++){
        printf("%d, %llu\n", i, each_nnz_block_count[i]);
    }

    free(nnz_each_block);
    free(each_nnz_block_count);
}